package com.dfrz.household.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dfrz.household.bean.User;
import com.dfrz.household.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService{
    @Autowired
    UserMapper userMapper;

    @Override
    public User getUserById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public List<User> getUsers() {
        return userMapper.selectList(null);
    }
    @Override
    public List<User> getUsersParm(Map<String, String> parm) {
        QueryWrapper queryWrapper=new QueryWrapper();
        if(parm.get("uname")!=null && !parm.get("uname").trim().equals("")){
            queryWrapper.like("uname",parm.get("uname"));
        }
        if(parm.get("gender")!=null && !parm.get("gender").trim().equals("")){
            queryWrapper.eq("gender",parm.get("gender"));
        }
        return userMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<User> getUserByPage(Map<String, String> parm, Integer pageNo, Integer pageSize) {
        IPage<User> page=new Page<>();
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        if(parm.get("uname")!=null && !parm.get("uname").trim().equals("")){
            QueryWrapper queryWrapper=new QueryWrapper();
            ///queryWrapper.eq("uname",parm.get("uname"))
            queryWrapper.like("uname",parm.get("uname"));
            userMapper.selectPage(page,queryWrapper);
        }

        return userMapper.selectPage(page,null);
    }

    @Override
    public int delete(Integer id) {
        int count=userMapper.deleteById(id);
        return count;
    }

    @Override
    public int save(User user) {
        int count=userMapper.insert(user);
        return count;
    }

    @Override
    public int update(User user) {
     int count= userMapper.updateByPrimaryKeySelective(user);
     return count;
    }

@Override
    public User queryOneByLoginName(String uname) {
        User u=userMapper.queryOneByLoginName(uname);
        return u;
    }
}
